home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch19 / prog1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  2.5 KB  |  118 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <math.h>
  5. #include <graphics.h>
  6.  
  7.  
  8. void main(void)
  9. {
  10. int i,j,k,l;
  11. int height, height2;
  12. int color;
  13. unsigned char x,y,z;
  14. int window, level;
  15. unsigned char bubblemass[70][70][70];
  16. unsigned char bubble[21][21][21];
  17. int graphdriver, graphmode, grerror;
  18.   
  19.   
  20.   
  21.   /*GET WINDOW AND LEVEL FROM USER*/
  22.  
  23.   window = -1;
  24.   level = -1;
  25.  
  26.   while((window > 255)||(window <= 0))
  27.     {
  28.     printf("Window(0-255):");
  29.     scanf("%d",&window);
  30.     }
  31.   
  32.   while((level < (window/2))||(level > (255 - window/2)))
  33.     {
  34.     printf("Level(%d-%d):", window/2, (255 - window/2 - 1));
  35.     scanf("%d",&level);
  36.     }
  37.   
  38.  
  39.   /*MAKE BUBBLE*/
  40.  
  41.   for(i=-10; i < 11; i++)
  42.     {
  43.     height = ceil(10*sin(acos((double)i/10)));
  44.     if(height != 0)
  45.       for(j=-height; j < height+1; j++)
  46.         {
  47.         height2 = ceil(height*sin(acos((double)j/height)));  
  48.         if(height2 !=0)
  49.           for(k=-height2; k < height2+1; k++)
  50.             bubble[i+10][j+10][k+10] = 1; 
  51.         }
  52.     
  53.     }
  54.  
  55.   
  56.   /*INITIATE BUBBLE MASS*/
  57.  
  58.   for(i=0; i < 70; i++)
  59.     for(j=0; j < 70; j++)
  60.       for(k=0; k < 70; k++)
  61.         bubblemass[i][j][k] = 0;
  62.  
  63.   
  64.   /*CREATE BUBBLE MASS*/
  65.  
  66.   for(l=0; l < 1400; l++)
  67.     { 
  68.     x = (unsigned char)(((double)(rand())*30/
  69.                          (double)RAND_MAX) + 10);
  70.     y = (unsigned char)(((double)(rand())*30/
  71.                          (double)RAND_MAX) + 10);
  72.     z = (unsigned char)(((double)(rand())*30/
  73.                          (double)RAND_MAX) + 10);
  74.  
  75.     for(i=0; i < 21; i++)
  76.       for(j=0; j < 21; j++)
  77.         for(k=0; k < 21; k++)
  78.           if(bubble[i][j][k] == 1)
  79.             if(bubblemass[x+i][y+j][z+k] < 255)
  80.               bubblemass[x+i][y+j][z+k] ++;
  81.     }
  82.  
  83.   
  84.   /*PLOT IMAGE WITH WINDOWING AND LEVELING*/
  85.  
  86.   graphdriver = VGA256;
  87.   graphmode = 0;
  88.   initgraph( &graphdriver, &graphmode, "");
  89.   grerror = graphresult();
  90.   if(grerror)
  91.     {
  92.     closegraph();
  93.     printf("Error Initializing Graphics Mode.\n");
  94.     exit(1);
  95.     }
  96.  
  97.   while(!kbhit())
  98.     {
  99.     for(i=0; i < 70; i++)
  100.       for(j=0; j < 70; j++)
  101.         for(k=0; k < 70; k++)
  102.           {
  103.           color = (int)bubblemass[i][j][k] - (level - window/2);
  104.           if(color < 0) 
  105.             color = 0;
  106.           if(color > window) 
  107.            color = 15;
  108.            else color = (int)floor((double)color/(double)window*15.0);
  109.           putpixel(j, k, color+16);
  110.           }
  111.   
  112.     }
  113.  
  114.   getch();
  115.   closegraph();
  116.  
  117. }
  118.